package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ActivitiConstants;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.VacationApplyMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.VacationApply;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.activiti.base.WorkFlowService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.BPMService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TaskLinkIamService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.VacationApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 休假申请服务层实现
 */
public class VacationApplyServiceImpl implements VacationApplyService {
    public static final String PROCESS_KEY1 = "leave";

    @Autowired
    private VacationApplyMapper applyMapper;
    @Autowired
    private TaskLinkIamService taskLinkIamService;
    @Autowired
    private BPMService bpmService;
    @Value("#{configProperties['username']}")
    private String bpmUserName;
    @Value("#{configProperties['password']}")
    private String bpmPassword;
    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 删除请假申请
     *
     * @param vacationApplyIds
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] vacationApplyIds) {
        if (vacationApplyIds.length > 0) {
            return applyMapper.deleteByPrimaryKey(vacationApplyIds);
        }
        return 0;
    }

    // TODO: 2017/3/15,0015

    /**
     * 新增请假申请并提交
     *
     * @param record
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public synchronized int insertSelective(VacationApply record) throws Exception {
        // 返回结果集
        int count = 0;
        if (null != record) {
            // 保存业务数据
            count += applyMapper.insertSelective(record);
        }
        //开启流程
        Map map = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(record.getApproveId());
        map.put(ActivitiConstants.MANUAL_PERSON, list);
        try {
            workFlowService.startProcessInstanceByKey(PROCESS_KEY1, record.getVacationApplyId(), map, record.getOperatorId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据id查询请假申请
     *
     * @param vacationApplyId 请假申请id
     * @return
     */
    @Override
    public VacationApply selectByPrimaryKey(String vacationApplyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("vacationApplyId", vacationApplyId);
        return applyMapper.selectByPrimaryKey(map);
    }

    /**
     * 流程操作
     *
     * @param record
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(VacationApply record, EmployeeVo employeeVo) {
        int count = 0;
        if (null != record) {
            //修改请假信息
            count += applyMapper.updateByPrimaryKeySelective(record);
            //控制流程走向
            Map map = new HashMap<>();
            //不过关同意驳回都走结束，如果不同意，用户只能新建一个申请。(流程缺陷，后续完善)
            map.put("cond1", 1);
            workFlowService.completeTask(record.getTaskId(), map, record.getComments(), employeeVo.getEmployeeId());
        }
        return count;
    }

    /**
     * 请假申请列表查询
     *
     * @param vacationApply
     * @return
     */
    @Override
    public JSONObject list(VacationApply vacationApply) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        vacationApply.setLimit(vacationApply.getLimit() == null ? Integer.valueOf(10) : vacationApply.getLimit());
        vacationApply.setOffset(vacationApply.getOffset() == null ? Integer.valueOf(0) : vacationApply.getOffset());
        // 总条数
        jsonObject.put("total", applyMapper.queryCount(vacationApply));
        List<VacationApply> trainList = applyMapper.queryList(vacationApply);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

    /**
     * 代办查找
     *
     * @param bootstrapSearchBean 查询参数
     * @return
     */
    public List<Object> searchByPIId(BootstrapSearchBean bootstrapSearchBean) {
        return applyMapper.searchByPIId(bootstrapSearchBean);
    }

    /**
     * 代办总条数
     *
     * @param bootstrapSearchBean
     * @return
     */
    @Override
    public int totalCountByPIId(BootstrapSearchBean bootstrapSearchBean) {
        return applyMapper.totalCountByPIId(bootstrapSearchBean);
    }

    /**
     * 查询我已经审批的
     *
     * @param vacationApply
     * @return
     */
    @Override
    public JSONObject processed(VacationApply vacationApply) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        vacationApply.setLimit(vacationApply.getLimit() == null ? Integer.valueOf(10) : vacationApply.getLimit());
        vacationApply.setOffset(vacationApply.getOffset() == null ? Integer.valueOf(0) : vacationApply.getOffset());
        // 总条数
        jsonObject.put("total", applyMapper.queryProcessedCount(vacationApply));
        List<VacationApply> trainList = applyMapper.queryProcessedList(vacationApply);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

    /**
     * 待我审批的请假
     *
     * @param vacationApply 查询条件
     */
    @Override
    public JSONObject listToDoTask(VacationApply vacationApply) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        vacationApply.setLimit(vacationApply.getLimit() == null ? Integer.valueOf(10) : vacationApply.getLimit());
        vacationApply.setOffset(vacationApply.getOffset() == null ? Integer.valueOf(0) : vacationApply.getOffset());
        // 总条数
        jsonObject.put("total", applyMapper.queryToDoVacationCount(vacationApply));
        List<VacationApply> trainList = applyMapper.queryToDoVacationList(vacationApply);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

}
